<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm" style="padding-bottom: 500px">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=engin_budbian','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=engin_budbian','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>原预算信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>原预算主题</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="budgetid" class="layui-input budgetid" onclick="choose_engin_budget()">
                            <div class="choose_div" id="budgetid" key="1"></div>
                            <span class="choose_one" onclick="choose_engin_budget()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">预算编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="budget_number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>预算变更信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>本次变更主题</label>
                        <div class="layui-input-block">
                            <input type="text" id="title" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>变更类型</label>
                        <div class="layui-input-block">
                            <select id="type" name="type" lay-filter="type" >
                                <option value="1">调整</option>
                                <option value="2">增项</option>
                                <option value="3">调整及增项</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto" id="tiaoDiv">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>调整明细</span></div>
                        <div>
                            <button type="button" onclick="choose_engin_budget_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从预算明细选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>预算分类</th>
                            <th>名称及规格</th>
                            <th>单位</th>
                            <th>预算数量</th>
                            <th>预算单价(元)</th>
                            <th>预算金额(元)</th>
                            <th><text>*</text>变更后数量</th>
                            <th><text>*</text>变更后单价(元)</th>
                            <th><text>*</text>变更后金额(元)</th>
                            <th>变更原因</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="tiao_list"></tbody>
                    </table>
                    <div class="listerror" id="tiao_listerror">提示：<span></span></div>
                </div>
            </div>


            <div class="layui-form-item height_auto layui-hide" id="zengDiv">
                <div class="short_table_list" key="2">
                    <div class="opt-btn-group">
                        <div><span>增项明细</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>预算分类</th>
                            <th><text>*</text>名称及规格</th>
                            <th><text>*</text>单位</th>
                            <th><text>*</text>预算数量</th>
                            <th><text>*</text>预算单价(元)</th>
                            <th>预算金额(元)</th>
                            <th>增项原因</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="zeng_list"></tbody>
                    </table>
                    <div class="listerror" id="zeng_listerror">提示：<span></span></div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    chu_flow_edit_yushen();

    $("#commentForm").validate({
        rules: {
            title: {required: true},
        },
        messages:{
            title: {required: "请输入"},
        }
    });


    function choose_engin_budget(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_engin_budget.php?dom=budgetid&page=engin_budbian&enginid='+enginid,'80%','80%')
    }

    function choose_engin_budget_list(obj){
        var budgetid = get_choose_val('budgetid');
        if(!budgetid){
            facade.error("请先选择预算主题");return false;
        }
        openPage('../public/choose_engin_budget_list.php?page=engin_budbian&choose_type=checkbox&budgetid='+budgetid,'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();
        facade.ajax({type: 'get', url: ask_flowdata_edit_engin_budbian, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                chu_flow_qian(data);

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }

                var budgetinfo = res.data.budgetinfo;
                if(budgetinfo){
                    set_choose_btn('budgetid',budgetinfo.id,budgetinfo.title);
                    $("#budget_number").val(budgetinfo.number);
                }

                $("#title").val(data.title);
                $("#type").val(data.type);
                $("#explain").val(data.explain);


                if(data.type == 1){
                    $('#tiaoDiv').removeClass('layui-hide');
                    $('#zengDiv').addClass('layui-hide');
                }
                if(data.type == 2){
                    $('#tiaoDiv').addClass('layui-hide');
                    $('#zengDiv').removeClass('layui-hide');
                }
                if(data.type == 3){
                    $('#tiaoDiv').removeClass('layui-hide');
                    $('#zengDiv').removeClass('layui-hide');
                }



                short_table_opt_func('',1);

                var budtype = res.data.budtype;
                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><select class="layui-select budtype" lay-filter="budtype">';
                listAddStr += '<option value="">请选择</option>';
                for (var mmm in budtype) {
                    listAddStr += '<option value="'+mmm+'">'+budtype[mmm]+'</option>';
                }
                listAddStr += '</select></td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input num"></td>';
                listAddStr += '<td><input type="number" class="layui-input price"></td>';
                listAddStr += '<td><input type="number" class="layui-input money disabled" disabled></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 2);



                var tiaolist = res.data.tiaolist;
                if(tiaolist.length > 0){
                    var str = '';
                    for (var key in tiaolist){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" listid="'+tiaolist[key].listid+'" budtype="'+tiaolist[key].budtype+'" value="'+tiaolist[key].budtype_name+'" class="layui-input budtype disabled" disabled></td>';
                        str += '<td><input type="text" disabled class="layui-input name disabled" value="'+tiaolist[key].name+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input unit disabled" value="'+tiaolist[key].unit+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input num disabled" value="'+tiaolist[key].num+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input price disabled" value="'+tiaolist[key].price+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input money disabled" value="'+tiaolist[key].money+'"></td>';
                        str += '<td><input type="number" class="layui-input new_num" value="'+tiaolist[key].new_num+'"></td>';
                        str += '<td><input type="number" class="layui-input new_price" value="'+tiaolist[key].new_price+'"></td>';
                        str += '<td><input type="number" disabled class="layui-input new_money disabled" value="'+tiaolist[key].new_money+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+tiaolist[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#tiao_list").html(str);
                }


                var zenglist = res.data.zenglist;
                if(zenglist.length > 0){
                    for (var key in zenglist){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><select class="layui-select budtype" lay-filter="budtype">';
                        str += '<option value="">请选择</option>';
                        for (var mmm in budtype) {
                            if(mmm == zenglist[key].budtype){
                                str += '<option value="'+mmm+'" selected>'+budtype[mmm]+'</option>';
                            }else{
                                str += '<option value="'+mmm.id+'">'+budtype[mmm]+'</option>';
                            }
                        }
                        str += '</select></td>';
                        str += '<td><input type="text" class="layui-input name" value="'+zenglist[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input unit" value="'+zenglist[key].unit+'"></td>';
                        str += '<td><input type="number" class="layui-input num" value="'+zenglist[key].num+'"></td>';
                        str += '<td><input type="number" class="layui-input price" value="'+zenglist[key].price+'"></td>';
                        str += '<td><input type="number" class="layui-input money disabled" value="'+zenglist[key].money+'" disabled></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+zenglist[key].explain+'"></td>';
                        str += '</tr>';
                        $("#zeng_list").append(str);
                    }
                }
                update_refresh_row_func()


                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('select(type)', function(data){
            if(data.value == 1){
                $('#tiaoDiv').removeClass('layui-hide');
                $('#zengDiv').addClass('layui-hide');
            }
            if(data.value == 2){
                $('#tiaoDiv').addClass('layui-hide');
                $('#zengDiv').removeClass('layui-hide');
            }
            if(data.value == 3){
                $('#tiaoDiv').removeClass('layui-hide');
                $('#zengDiv').removeClass('layui-hide');
            }
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            parseMoney(data);

            data.field.enginid = get_choose_val('enginid');
            data.field.budgetid = get_choose_val('budgetid');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                formValid('budgetid');
                if(data.field.budgetid == undefined || !data.field.budgetid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }

            var tiaolist = [''];
            if(data.field.type == 1 || data.field.type == 3){
                try {
                    $('#tiao_list tr').each(function (i) {
                        var listid = $(this).find('.budtype').attr('listid') || 0;
                        var budtype = $(this).find('.budtype').attr('budtype') || '';
                        var name = $(this).find('.name').val();
                        var unit = $(this).find('.unit').val();
                        var num = $(this).find('.num').val();
                        var price = $(this).find('.price').val();
                        var money = $(this).find('.money').val();
                        var new_num = $(this).find('.new_num').val();
                        var new_price = $(this).find('.new_price').val();
                        var new_money = $(this).find('.new_money').val();
                        var explain = $(this).find('.explain').val();

                        var index = i + 1;

                        if(data.field.isturn > 0){
                            if(!new_num){
                                throw new Error('第'+index+'行，请输入变更后数量');
                            }
                            if(!new_price){
                                throw new Error('第'+index+'行，请输入变更后单价');
                            }

                            var chongKey = check_tiao_chongfu(i, listid);
                            if(chongKey){
                                throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                            }
                        }


                        tiaolist.push({
                            listid:listid,
                            budtype:budtype,
                            name:name,
                            unit:unit,
                            num:num,
                            price:price,
                            money:money,
                            new_num:new_num,
                            new_price:new_price,
                            new_money:new_money,
                            explain:explain,
                        });
                    });
                } catch(e) {
                    $("#tiao_listerror").find('span').text(e.message);
                    $("#tiao_listerror").show();
                    return false;
                }
                $("#tiao_listerror").hide();
            }
            data.field.tiaolist = tiaolist;


            var zenglist = [''];
            if(data.field.type == 2 || data.field.type == 3){
                try {
                    $('#zeng_list tr').each(function (i) {
                        var listid = 0;
                        var budtype = $(this).find('.budtype').val();
                        var name = $(this).find('.name').val();
                        var unit = $(this).find('.unit').val();
                        var num = $(this).find('.num').val();
                        var price = $(this).find('.price').val();
                        var money = $(this).find('.money').val();
                        var explain = $(this).find('.explain').val();

                        var index = i + 1;

                        if(data.field.isturn > 0){
                            if(!budtype){
                                throw new Error('第'+index+'行，请选择预算分类');
                            }
                            if(!name){
                                throw new Error('第'+index+'行，请输入名称及规格');
                            }
                            if(!unit){
                                throw new Error('第'+index+'行，请输入单位');
                            }
                            if(num==''){
                                throw new Error('第'+index+'行，请输入预算数量');
                            }
                            if(price==''){
                                throw new Error('第'+index+'行，请输入预算单价');
                            }
                        }


                        zenglist.push({
                            budtype:budtype,
                            name:name,
                            unit:unit,
                            num:num,
                            price:price,
                            money:money,
                            explain:explain,
                        });
                    });
                } catch(e) {
                    $("#zeng_listerror").find('span').text(e.message);
                    $("#zeng_listerror").show();
                    return false;
                }
                $("#zeng_listerror").hide();
            }
            data.field.zenglist = zenglist;

            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_engin_budbian, ask_flowdata_edit_engin_budbian, $(this).attr('but'));
            return false;
        });

    });





    //检测重复
    function check_tiao_chongfu(index, listid){
        try {
            $('#tiao_list tr').each(function (i) {
                var cur_listid = $(this).find('.budtype').attr('listid') || 0;
                if(index != i){
                    if(listid === cur_listid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }
        return 0;
    }




    $('#tiao_list').on('input propertychange','.new_num,.new_price',function () {
        auto_tiao_money();
    });
    function auto_tiao_money() {
        $('#tiao_list tr').each(function (i) {
            var new_num = parseValidNum($(this).find('.new_num').val());
            var new_price = parseValidNum($(this).find('.new_price').val());
            var new_money = parseFloat(new_num) * parseFloat(new_price).toFixed(2);
            $(this).find('.new_money').val(new_money);
        });
    }





    $('#zeng_list').on('input propertychange','.num,.price',function () {
        auto_zeng_money();
    });
    function auto_zeng_money() {
        $('#zeng_list tr').each(function (i) {
            var num = parseValidNum($(this).find('.num').val());
            var price = parseValidNum($(this).find('.price').val());
            var money = parseFloat(num) * parseFloat(price).toFixed(2);
            $(this).find('.money').val(money);
        });
    }

</script>
</body>
</html>
